<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.4"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>miniFOC: algorithm/foc.c File Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectlogo"><img alt="Logo" src="Acme_LOGO.png"/></td>
  <td id="projectalign">
   <div id="projectname">miniFOC<span id="projectnumber">&#160;1.0.3</span>
   </div>
   <div id="projectbrief">This open-source project aims to accomplish a FOC(Field Oriented Control) scheme that is operatable with minimum costs in China.</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.4 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search",'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_99d3723365464dd656f512988bb8dbbd.html">algorithm</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="summary">
<a href="#func-members">Functions</a> &#124;
<a href="#var-members">Variables</a>  </div>
  <div class="headertitle"><div class="title">foc.c File Reference</div></div>
</div><!--header-->
<div class="contents">

<p>this document is mainly the code implementation of motor phase sequence correction and FOC algorithm.  
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &quot;<a class="el" href="foc_8h_source.html">foc.h</a>&quot;</code><br />
<code>#include &quot;<a class="el" href="config_8h_source.html">config.h</a>&quot;</code><br />
<code>#include &quot;<a class="el" href="fast__math_8h_source.html">fast_math.h</a>&quot;</code><br />
<code>#include &quot;<a class="el" href="encoder_8h_source.html">encoder.h</a>&quot;</code><br />
<code>#include &quot;<a class="el" href="system_8h_source.html">system.h</a>&quot;</code><br />
<code>#include &quot;<a class="el" href="timer_8h_source.html">timer.h</a>&quot;</code><br />
</div>
<p><a href="foc_8c_source.html">Go to the source code of this file.</a></p>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a85fbd426ff2859593ed2b1ecd4a35d4b"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="foc_8c.html#a85fbd426ff2859593ed2b1ecd4a35d4b">foc_calibrate_phase</a> (void)</td></tr>
<tr class="memdesc:a85fbd426ff2859593ed2b1ecd4a35d4b"><td class="mdescLeft">&#160;</td><td class="mdescRight">automatic phase sequence detection and correction  <a href="foc_8c.html#a85fbd426ff2859593ed2b1ecd4a35d4b">More...</a><br /></td></tr>
<tr class="separator:a85fbd426ff2859593ed2b1ecd4a35d4b"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a2ee2669a464e17cfaab4e084e78c4fe1"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="foc_8c.html#a2ee2669a464e17cfaab4e084e78c4fe1">foc_calculate_dutycycle</a> (float elect_angle, float d, float q, float *u, float *v, float *w)</td></tr>
<tr class="memdesc:a2ee2669a464e17cfaab4e084e78c4fe1"><td class="mdescLeft">&#160;</td><td class="mdescRight">calculate the corresponding three-phase PWM duty cycle under the current electrical angle  <a href="foc_8c.html#a2ee2669a464e17cfaab4e084e78c4fe1">More...</a><br /></td></tr>
<tr class="separator:a2ee2669a464e17cfaab4e084e78c4fe1"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="var-members" name="var-members"></a>
Variables</h2></td></tr>
<tr class="memitem:a6c9e1e8356380a4129a3a59d52ee1ec4"><td class="memItemLeft" align="right" valign="top">volatile <a class="el" href="struct_f_o_c___structure__t.html">FOC_Structure_t</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="foc_8c.html#a6c9e1e8356380a4129a3a59d52ee1ec4">FOC_Struct</a></td></tr>
<tr class="memdesc:a6c9e1e8356380a4129a3a59d52ee1ec4"><td class="mdescLeft">&#160;</td><td class="mdescRight">FOC handler.  <a href="foc_8c.html#a6c9e1e8356380a4129a3a59d52ee1ec4">More...</a><br /></td></tr>
<tr class="separator:a6c9e1e8356380a4129a3a59d52ee1ec4"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ad335385ea3909d9485703d97735b34ca"><td class="memItemLeft" align="right" valign="top">volatile unsigned char&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="foc_8c.html#ad335385ea3909d9485703d97735b34ca">phase_sequence</a> = 0</td></tr>
<tr class="memdesc:ad335385ea3909d9485703d97735b34ca"><td class="mdescLeft">&#160;</td><td class="mdescRight">motor phase sequence flag variable  <a href="foc_8c.html#ad335385ea3909d9485703d97735b34ca">More...</a><br /></td></tr>
<tr class="separator:ad335385ea3909d9485703d97735b34ca"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a418bbe3f3019eaefea9f904a87cf0a05"><td class="memItemLeft" align="right" valign="top">unsigned char&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="foc_8c.html#a418bbe3f3019eaefea9f904a87cf0a05">foc_parameter_available_flag</a> = 1</td></tr>
<tr class="memdesc:a418bbe3f3019eaefea9f904a87cf0a05"><td class="mdescLeft">&#160;</td><td class="mdescRight">flag variable for FOC parameter availability  <a href="foc_8c.html#a418bbe3f3019eaefea9f904a87cf0a05">More...</a><br /></td></tr>
<tr class="separator:a418bbe3f3019eaefea9f904a87cf0a05"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p >this document is mainly the code implementation of motor phase sequence correction and FOC algorithm. </p>
<dl class="section author"><dt>Author</dt><dd>Lao·Zhu </dd></dl>
<dl class="section version"><dt>Version</dt><dd>V1.0.3 </dd></dl>
<dl class="section date"><dt>Date</dt><dd>1. August 2023 </dd></dl>

<p class="definition">Definition in file <a class="el" href="foc_8c_source.html">foc.c</a>.</p>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="a2ee2669a464e17cfaab4e084e78c4fe1" name="a2ee2669a464e17cfaab4e084e78c4fe1"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a2ee2669a464e17cfaab4e084e78c4fe1">&#9670;&nbsp;</a></span>foc_calculate_dutycycle()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void foc_calculate_dutycycle </td>
          <td>(</td>
          <td class="paramtype">float&#160;</td>
          <td class="paramname"><em>elect_angle</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">float&#160;</td>
          <td class="paramname"><em>d</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">float&#160;</td>
          <td class="paramname"><em>q</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">float *&#160;</td>
          <td class="paramname"><em>u</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">float *&#160;</td>
          <td class="paramname"><em>v</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">float *&#160;</td>
          <td class="paramname"><em>w</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">

<p>calculate the corresponding three-phase PWM duty cycle under the current electrical angle </p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramdir">[in]</td><td class="paramname">elect_angle</td><td>electrical angle of motor rotor, in radian system </td></tr>
    <tr><td class="paramdir">[in]</td><td class="paramname">d</td><td>expected field strength in the direct axis direction of the motor </td></tr>
    <tr><td class="paramdir">[in]</td><td class="paramname">q</td><td>expected field strength in the quadrature axis direction of the motor </td></tr>
    <tr><td class="paramdir">[out]</td><td class="paramname">u</td><td>calculation results of U-phase duty cycle </td></tr>
    <tr><td class="paramdir">[out]</td><td class="paramname">v</td><td>calculation results of V-phase duty cycle </td></tr>
    <tr><td class="paramdir">[out]</td><td class="paramname">w</td><td>calculation results of W-phase duty cycle </td></tr>
  </table>
  </dd>
</dl>

<p class="definition">Definition at line <a class="el" href="foc_8c_source.html#l00099">99</a> of file <a class="el" href="foc_8c_source.html">foc.c</a>.</p>

<p class="reference">References <a class="el" href="fast__math_8h_source.html#l00027">fast_cos</a>, <a class="el" href="fast__math_8c_source.html#l00072">fast_sin()</a>, and <a class="el" href="config_8h_source.html#l00013">VBUS</a>.</p>

<p class="reference">Referenced by <a class="el" href="encoder_8c_source.html#l00119">encoder_zeroing()</a>, <a class="el" href="foc_8c_source.html#l00033">foc_calibrate_phase()</a>, and <a class="el" href="gd32f1x0__it_8c_source.html#l00104">TIMER2_IRQHandler()</a>.</p>

</div>
</div>
<a id="a85fbd426ff2859593ed2b1ecd4a35d4b" name="a85fbd426ff2859593ed2b1ecd4a35d4b"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a85fbd426ff2859593ed2b1ecd4a35d4b">&#9670;&nbsp;</a></span>foc_calibrate_phase()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void foc_calibrate_phase </td>
          <td>(</td>
          <td class="paramtype">void&#160;</td>
          <td class="paramname"></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">

<p>automatic phase sequence detection and correction </p>

<p class="definition">Definition at line <a class="el" href="foc_8c_source.html#l00033">33</a> of file <a class="el" href="foc_8c_source.html">foc.c</a>.</p>

<p class="reference">References <a class="el" href="config_8h_source.html#l00019">CALI_TORQUE</a>, <a class="el" href="system_8c_source.html#l00030">delayms()</a>, <a class="el" href="encoder_8c_source.html#l00076">encoder_get_mechanical_angle()</a>, <a class="el" href="foc_8c_source.html#l00099">foc_calculate_dutycycle()</a>, <a class="el" href="foc_8c_source.html#l00024">phase_sequence</a>, and <a class="el" href="timer_8c_source.html#l00020">update_pwm_dutycycle()</a>.</p>

<p class="reference">Referenced by <a class="el" href="main_8c_source.html#l00139">main()</a>.</p>

</div>
</div>
<h2 class="groupheader">Variable Documentation</h2>
<a id="a418bbe3f3019eaefea9f904a87cf0a05" name="a418bbe3f3019eaefea9f904a87cf0a05"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a418bbe3f3019eaefea9f904a87cf0a05">&#9670;&nbsp;</a></span>foc_parameter_available_flag</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">unsigned char foc_parameter_available_flag = 1</td>
        </tr>
      </table>
</div><div class="memdoc">

<p>flag variable for FOC parameter availability </p>

<p class="definition">Definition at line <a class="el" href="foc_8c_source.html#l00028">28</a> of file <a class="el" href="foc_8c_source.html">foc.c</a>.</p>

<p class="reference">Referenced by <a class="el" href="flash_8c_source.html#l00046">flash_read_parameters()</a>, <a class="el" href="main_8c_source.html#l00028">mdtp_callback_handler()</a>, and <a class="el" href="report_8c_source.html#l00015">report_local_variable()</a>.</p>

</div>
</div>
<a id="a6c9e1e8356380a4129a3a59d52ee1ec4" name="a6c9e1e8356380a4129a3a59d52ee1ec4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a6c9e1e8356380a4129a3a59d52ee1ec4">&#9670;&nbsp;</a></span>FOC_Struct</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">volatile <a class="el" href="struct_f_o_c___structure__t.html">FOC_Structure_t</a> FOC_Struct</td>
        </tr>
      </table>
</div><div class="memdoc">

<p>FOC handler. </p>

<p class="definition">Definition at line <a class="el" href="foc_8c_source.html#l00020">20</a> of file <a class="el" href="foc_8c_source.html">foc.c</a>.</p>

<p class="reference">Referenced by <a class="el" href="encoder_8c_source.html#l00093">encoder_get_electronic_angle()</a>, <a class="el" href="encoder_8c_source.html#l00106">encoder_update_speed()</a>, <a class="el" href="main_8c_source.html#l00028">mdtp_callback_handler()</a>, <a class="el" href="report_8c_source.html#l00069">report_angle_speed()</a>, <a class="el" href="report_8c_source.html#l00015">report_local_variable()</a>, <a class="el" href="gd32f1x0__it_8c_source.html#l00124">TIMER13_IRQHandler()</a>, and <a class="el" href="gd32f1x0__it_8c_source.html#l00104">TIMER2_IRQHandler()</a>.</p>

</div>
</div>
<a id="ad335385ea3909d9485703d97735b34ca" name="ad335385ea3909d9485703d97735b34ca"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ad335385ea3909d9485703d97735b34ca">&#9670;&nbsp;</a></span>phase_sequence</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">volatile unsigned char phase_sequence = 0</td>
        </tr>
      </table>
</div><div class="memdoc">

<p>motor phase sequence flag variable </p>

<p class="definition">Definition at line <a class="el" href="foc_8c_source.html#l00024">24</a> of file <a class="el" href="foc_8c_source.html">foc.c</a>.</p>

<p class="reference">Referenced by <a class="el" href="flash_8c_source.html#l00046">flash_read_parameters()</a>, <a class="el" href="flash_8c_source.html#l00026">flash_write_parameters()</a>, <a class="el" href="foc_8c_source.html#l00033">foc_calibrate_phase()</a>, <a class="el" href="report_8c_source.html#l00015">report_local_variable()</a>, and <a class="el" href="gd32f1x0__it_8c_source.html#l00104">TIMER2_IRQHandler()</a>.</p>

</div>
</div>
</div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.4
</small></address>
</body>
</html>
